package com.cynoware.busappmas.contentprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;


public class DbContentProvider extends ContentProvider {
	
	private static final String PROVIDER_NAME  ="com.cynoware.busappmas.contentprovider"; 
 
	public  static final Uri CONTENT_URI_CONFIGS   = Uri.parse( "content://" + PROVIDER_NAME + "/configs");
	public  static final Uri CONTENT_URI_VIDEOLIST = Uri.parse( "content://" + PROVIDER_NAME + "/videolist");
	public  static final Uri CONTENT_URI_PLAYTIME  = Uri.parse( "content://" + PROVIDER_NAME + "/playtime");
	public  static final Uri CONTENT_URI_STOPTIME  = Uri.parse( "content://" + PROVIDER_NAME + "/stoptime");
	public  static final Uri CONTENT_URI_CUTPLAY   = Uri.parse( "content://" + PROVIDER_NAME + "/cutplay");
	public  static final Uri CONTENT_URI_TIMINGAD  = Uri.parse( "content://" + PROVIDER_NAME + "/timingad");
	public  static final Uri CONTENT_URI_GPRSAD    = Uri.parse( "content://" + PROVIDER_NAME + "/gprsad");
	public  static final Uri CONTENT_URI_GPSLOCAL  = Uri.parse( "content://" + PROVIDER_NAME + "/gpslocalinfo");
	public  static final Uri CONTENT_URI_WORKTIME  = Uri.parse( "content://" + PROVIDER_NAME + "/worktime");
	public  static final Uri CONTENT_URI_ADMIN     = Uri.parse( "content://" + PROVIDER_NAME + "/admin");
	public  static final Uri CONTENT_URI_STAPICTURE    = Uri.parse( "content://" + PROVIDER_NAME + "/stapicture");
	public  static final Uri CONTENT_URI_PEAKVOLUME     = Uri.parse( "content://" + PROVIDER_NAME + "/peakvolume");
	
	private DbHelper mDbHelper;
	private SQLiteDatabase  mDatabase;
	
	private static final int CONFIS     = 1;
	private static final int VIDEO_LIST = 3;
	private static final int PLAY_TIME  = 5;
	private static final int CUT_PLAY   = 7;
	private static final int TIMING_AD  = 9;
	private static final int GPRS_AD    = 11;
	private static final int WORKTIME   = 13;
	private static final int ADMIN      = 15;
	private static final int STOPTIME   = 17;
	private static final int GPSLOCALINFO = 19;
	private static final int STAPICTURE = 20;
	private static final int PEAKVOLUME = 21;
	
	private static final UriMatcher mUriMatcher;
	
	static{
		mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		mUriMatcher.addURI(PROVIDER_NAME, "configs"  , CONFIS);
		mUriMatcher.addURI(PROVIDER_NAME, "videolist", VIDEO_LIST);
		mUriMatcher.addURI(PROVIDER_NAME, "playtime" , PLAY_TIME);
		mUriMatcher.addURI(PROVIDER_NAME, "cutplay"  , CUT_PLAY);
		mUriMatcher.addURI(PROVIDER_NAME, "timingad" , TIMING_AD);
		mUriMatcher.addURI(PROVIDER_NAME, "gprsad"   , GPRS_AD);
		mUriMatcher.addURI(PROVIDER_NAME, "worktime" , WORKTIME);
		mUriMatcher.addURI(PROVIDER_NAME, "admin"    , ADMIN);
		mUriMatcher.addURI(PROVIDER_NAME, "stoptime" , STOPTIME);
		mUriMatcher.addURI(PROVIDER_NAME, "gpslocalinfo" , GPSLOCALINFO);
		mUriMatcher.addURI(PROVIDER_NAME, "stapicture" , STAPICTURE);
		mUriMatcher.addURI(PROVIDER_NAME, "peakvolume" , PEAKVOLUME);
	}

	
	private String UriToTable(Uri uri){
		String table = null;
		switch (mUriMatcher.match(uri)) {
		case CONFIS:
			table = DbHelper.TABLE_CONFIGS;
			break;
		case VIDEO_LIST:
			table = DbHelper.TABLE_VIDEOLIST;
			break;
		case PLAY_TIME:
			table = DbHelper.TABLE_PLAYTIME;
			break;
		case CUT_PLAY:
			table = DbHelper.TABLE_CUTPLAY;
			break;
		case TIMING_AD:
			table = DbHelper.TABLE_TIMINGAD;
			break;
		case GPRS_AD:
			table = DbHelper.TABLE_GPRSAD;
			break;
		case WORKTIME:
			table = DbHelper.TABLE_WORKINGTIME;
			break;
		case ADMIN:
			table = DbHelper.TABLE_ADMIN;
			break;
		case STOPTIME:
			table = DbHelper.TABLE_STOPTIME;
			break;
		case GPSLOCALINFO:
			table = DbHelper.TABLE_GPSLOCALINFO;
			break;
		case STAPICTURE:
			table = DbHelper.TABLE_STAPICTURE;
			break;
		case PEAKVOLUME:
			table = DbHelper.TABLE_PEAKVOLUME;
			break;
		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}
		return table;
	}

	
	@Override
	public boolean onCreate() {
		mDbHelper = new DbHelper(getContext());
		mDatabase = mDbHelper.getReadableDatabase();
		return false;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		String table = UriToTable(uri);
		qb.setTables(table);		
		Cursor cursor = qb.query(mDatabase, projection, selection, selectionArgs, null, null, sortOrder);
		cursor.setNotificationUri(getContext().getContentResolver(), uri);
		return cursor;
		
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		
		String table = UriToTable(uri);	
 		mDatabase.insert(table, null, values); 		
		return null;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		
		String table = UriToTable(uri);	
 		return mDatabase.delete(table, selection, selectionArgs); 		

	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		
		String table = UriToTable(uri);	
		return mDatabase.update(table, values, selection, selectionArgs);
		
	}

}